import { Component, OnInit, OnDestroy } from '@angular/core'
import { INewPageColumn } from '@shared/interfaces/common'
import { ComWindow } from '@shared/classes/ComWindowClass'
import { AcquirerNewTradeComponent } from '../../new/trade/trade.component'
import { Acquirer, IUpdateAcquirerTradeInfo } from '@shared/interfaces/acquirer'
import { deepCopy } from '@delon/util'
import { NzMessageService } from 'ng-zorro-antd'
import { ActivatedRoute } from '@angular/router'
import { AcquirerService } from '../../acquirer.service'
import { AcquirerStoreService } from '../../new/acquirer-store.service'
import { CommonAcquirerService } from '@shared/services/acquirer.service'
import { PageService } from '@shared/services/page.service'
import { Location } from '@angular/common'
import { of } from 'rxjs'
import { mergeMap } from 'rxjs/operators'
import { I18NService } from '@core'

@Component({
  selector: 'app-edit-trade',
  templateUrl: './trade.component.html',
})
export class AcquirerAddTradeComponent implements OnInit, OnDestroy {

  pages: INewPageColumn[] = []
  insCode: string

  constructor(
    private msg: NzMessageService,
    private route: ActivatedRoute,
    private location: Location,
    private acquirerService: AcquirerService,
    private acquirerStoreService: AcquirerStoreService,
    private commonAcquirerService: CommonAcquirerService,
    private pageService: PageService,
    private i18n: I18NService,
  ) {}

  ngOnInit() {
    this.insCode = this.getInsCode()
    this.pageService.setTradeEdit(true)
    this.initData()
  }

  ngOnDestroy() {
    this.pageService.setTradeEdit(false)
    this.acquirerStoreService.reset()
  }

  getInsCode(): string {
    return this.route.snapshot.params['id']
  }

  initData(): void {
    const viewingInsCode = this.commonAcquirerService.viewingAcquirer.institution.common.insCode
    of(viewingInsCode).pipe(
      mergeMap((insCode) => {
        return !insCode
          ? this.acquirerService.getOne(this.insCode)
          : of(this.commonAcquirerService.viewingAcquirer)
      })
    ).subscribe(
      (acquirer) => {
        const cloneAcquirer: Acquirer = deepCopy(acquirer)
        this.acquirerStoreService.updateValue(cloneAcquirer)
        this.initColumns()
      }
    )
  }

  initColumns(): void {
    this.pages = [
      {
        component: new ComWindow(AcquirerNewTradeComponent, { title: '编辑交易信息' })
      }
    ]
  }

  submit(): void {
    const acquirer = this.acquirerStoreService.value
    const { version } = acquirer.institution
    const { tradeInfo, insCode } = acquirer.institution.common
    const reqData: IUpdateAcquirerTradeInfo = {
      version,
      trade: tradeInfo,
    }
    this.acquirerService.updateTradeInfoOfAcquirer(reqData, insCode).subscribe(
      (res) => {
        this.msg.success(this.trans('GENERAL.NOTIFICATION.UPDATE_SUCCESS'))
        this.location.back()
      },
      (err) => {
        const { error } = err
        this.msg.error(error.msg)
      }
    )
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
